<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/5
 * Time: 20:23
 */

namespace Admin\Controller;

use Think\Controller;

/**
 * Class UserController
 * @package Admin\Controller
 * 用户管理
 */
class UserController extends Controller
{
   /**
    * 用户列表
    */
    public  function lists(){
        //查找旧数据
        $users=M('admin')->select();
        //找出用户所在的组
        foreach ($users as $k=>$v) {
         $users[$k]['group']=M('authGroupAccess')->join("sp_auth_group ON sp_auth_group_access.group_id=sp_auth_group.id")
             ->where("sp_auth_group_access.uid={$v['uid']}")->getField("sp_auth_group.title");
        }
        $this->assign('users',$users);
//        dd($users);
      $this->display();
    }
    /**
     * 用户添加
     */
    public function adduser(){
        //找出所有的权限组
        $groups=M('authGroup')->select();
        foreach($groups as $k=>$v){
            $groups[$k]['rulename']=M('authRule')->where("id","IN",$groups[$k]['rules'])->getField('title',true);
        }
        $this->assign('groups',$groups);
//        dd($groups);
        if(IS_POST){
//              dd(I('post.'));die;
            if(notNull(I('post.aname'))){
              $this->error('用户名不能为空');die;
            }
         if(!empty(I('post.upass'))){
           $data['apass']=md5(I('post.apass'));
         }
            $data['logintime']=time();
            $data['aname']=I('post.aname');
            $res=D('admin')->adduser($data);
            //为该用户绑定权限组
            $datas['uid']=$res['valid'];
            $datas['group_id']=I('post.group_id');
            $res=M('authGroupAccess')->add($datas);
            if($res){
                 $this->success('添加成功',U('lists'));die;
            }else{
                $this->error('添加失败');die;
            }
        }
    $this->display();
    }
    /**
     * 用户编辑
     */
    public  function edituser(){
        $uid=I('get.uid');
        if(IS_POST){
//            dd($_POST);die;
            if(notNull(I('post.aname'))){
                 $this->error('用户名不能为空');die;
            }
            $res= M('admin')->where("uid=$uid")->setField("aname",I('post.aname'));
           $res= M('authGroupAccess')->where("uid=$uid")->setField("group_id",I('post.group_id'));
            if($res){
                $this->success('编辑成功',U('lists'));die;
            }else{
                $this->error('编辑失败',U('lists'));die;
            }
        }
        //查找旧数据
        $olddata=M('admin')->where("uid=$uid")->find();
        $olddata['group']=M('authGroupAccess')->join("sp_auth_group ON sp_auth_group_access.group_id=sp_auth_group.id")
            ->where("sp_auth_group_access.uid=$uid")->getField("sp_auth_group.id");
        $olddata['groups']=M('authGroup')->select();
        foreach($olddata['groups'] as $k=>$v){
            $olddata['groups'][$k]['rulename']=M('authRule')->where("id","IN",$olddata['groups'][$k]['rules'])->getField("title",true);
        }
        $this->assign('olddata',$olddata);
//        dd($olddata);
        $this->display();
    }
    //删除用户
    public function deluser(){
        $uid=I('get.uid');
        M('admin')->where("uid=$uid")->delete();
        $res=M('authGroupAccess')->where("uid=$uid")->delete();
        if($res){
          $this->success('删除成功');die;
        }else{
            $this->error('删除失败');die;
        }
//        dd($uid);
    }
}